<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoloader()
    {
        $front = $this->bootstrap("FrontController")->getResource('FrontController');
        $modules = $front->getControllerDirectory();
        $default = $front->getDefaultModule();
        foreach (array_keys($modules) as $module) {
            if ($module === $default) {
                continue;
            }
            $moduleloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => ucfirst($module),
                'basePath'  => $front->getModuleDirectory($module)));
            $moduleloader->addResourceType('task', 'tasks', 'Task');
        }
    }

    protected function _initLog ()
    {
        if (!$this->hasOption('log')) return ;
        $config = $this->getOption('log');
        foreach ($config as $key => $option) {
            if (empty($option['enabled'])) {
                unset($config[$key]);
                continue;
            }
            if ($key == 'db') {
                $this->bootstrap('db');
                $db = Zend_Db_Table::getDefaultAdapter();
                $config[$key] = array_merge(array(
                    'writerName'   => 'Db',
                    'writerParams' => array(
                        'db'        => $db,
                        'table'     => 'sys_log',
                        'columnMap' => array(
                            'priority' => 'priority',
                            'message'  => 'message',
                            'name'     => 'priorityName',
                            'ltime'    => 'timestamp',
                            'employee' => 'employee'
                        )
                    )
                ), $config[$key]);
            }
        }

        $config['timestampFormat'] = 'Y-m-d H:i:s';

        $log = IG_Log::factory($config);

        return $log;
    }

    /**
     * 注册配置文件中的选项
     */
    protected function _initOptions()
    {
        $options = $this->getOption('options');
        Zend_Registry::set('lvan_application', $this->getOptions());
        Zend_Registry::set('lvan_options', (object) $options);
        Zend_Registry::set(
            'lvan_cache',
            $this->bootstrap("cacheManager")
                 ->getResource('cacheManager')
                 ->getCache('default')
        );
    }
}